package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.DeliveryCommandSheetLinesMoEntity;
import cn.com.huzo.repository.trade.DeliveryCommandSheetHeaderRepository;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.com.huzo.repository.trade.DeliveryCommandSheetLinesMoRepository;
import cn.com.huzo.repository.trade.DeliveryCommandSheetLinesRepository;
import cn.com.huzo.service.trade.DeliveryCommandSheetLinesMoManager;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货发令单明细--引入的生产指示书管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("DeliveryCommandSheetLinesMoManager")
public class DeliveryCommandSheetLinesMoManagerImpl extends GenericManagerImpl<DeliveryCommandSheetLinesMoEntity, String>
        implements DeliveryCommandSheetLinesMoManager {

    @Resource(name = "DeliveryCommandSheetHeaderRepository")
    private DeliveryCommandSheetHeaderRepository _dao_header;

    @Resource(name = "DeliveryCommandSheetLinesMoRepository")
    private DeliveryCommandSheetLinesMoRepository _dao_lines_mo;

    @Resource(name = "DeliveryCommandSheetLinesRepository")
    private DeliveryCommandSheetLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchByHeaderId(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderId(headerId));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchInsert(List<DeliveryCommandSheetLinesMoEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step1: 增加引入的生产指示书
        Integer rowsAffected = _dao_lines_mo.batchInsert(itemList);
        if (rowsAffected > 0) {
            itemList.stream().forEach((item) -> {
                // step2: 增加发货发令单明细
                _dao_lines.insert(item);
            });
            // 插入新的款号顺序
            _dao_lines.deleteLinesStyleNoOrder(itemList.get(0).getHeaderId());
            _dao_lines.insertLinesStyleNoOrder(itemList.get(0).getHeaderId());
            /*
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(),
                    "贸易信息→发货发令单引入生产指示书", "单据号：" + itemList.get(0).getBillNo(),
                    "增加本次发货生产情况明细");*/
        }
        // step3: 取得返回值
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderId(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(itemList.get(0).getHeaderId()));
        _result.put("SoNo", _dao_header.fetchSoNoByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(List<DeliveryCommandSheetLinesMoEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step1: 更新生产指示书
        Integer rowsAffected = _dao_lines_mo.batchUpdate(itemList);
        if (rowsAffected > 0) {
            itemList.stream().forEach((item) -> {
                // 删除对应的发令单明细数据
                Map<String, Object> criterion = new HashMap();
                criterion.put("HeaderId", item.getHeaderId());
                criterion.put("MoHeaderId", item.getMoHeaderId());
                _dao_lines.deleteByLinesMo(criterion);

                // step2: 增加发货发令单明细
                _dao_lines.insert(item);
            });
            _dao_lines.deleteLinesStyleNoOrder(itemList.get(0).getHeaderId());
            _dao_lines.insertLinesStyleNoOrder(itemList.get(0).getHeaderId());
            // 审计日志
            //writeAuditLog(itemList.get(0).getUpdatedBy(),
            //        "贸易信息→发货发令单引入生产指示书", "单据号：" + itemList.get(0).getBillNo(),
            //        "更新本次发货生产情况明细");
        }
        // step3: 取得返回值
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderId(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(itemList.get(0).getHeaderId()));
        _result.put("SoNo", _dao_header.fetchSoNoByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> deleteLines(DeliveryCommandSheetLinesMoEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step1: 删除引入的生产指示书
        Integer rowsAffected = _dao_lines_mo.delete(instance.getLinesId());
        if (rowsAffected > 0) {
            // 删除对应的发令单明细数据
            Map<String, Object> criterion = new HashMap();
            criterion.put("HeaderId", instance.getHeaderId());
            criterion.put("MoHeaderId", instance.getMoHeaderId());
            _dao_lines.deleteByLinesMo(criterion);
            _dao_lines.deleteLinesStyleNoOrder(instance.getHeaderId());
            // 审计日志
            //writeAuditLog(instance.getDeletedBy(), "贸易信息→发货发令单引入生产指示书",
            //        "订单合同号：" + instance.getSoNo() + "　款号：" + instance.getStyleNo(),
            //        "删除本次发货生产情况明细");
        }
        // step3: 取得返回值
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderId(instance.getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(instance.getHeaderId()));
        // 主合同号
        _result.put("SoNo", _dao_header.fetchSoNoByHeaderKey(instance.getHeaderId()));

        return _result;
    }
}
